package cn.hh.org.controller;

import cn.hh.basic.util.AjaxResult;
import cn.hh.basic.util.PageList;
import cn.hh.org.domain.Shop;
import cn.hh.org.query.ShopQuery;
import cn.hh.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/shop")
public class ShopController {
    @Autowired
    private IShopService shopService;
    @PostMapping("/settlement")
    public AjaxResult addOrUpdate(@RequestBody Shop shop){
        /*通过id判断是新增还是删除*/
        try {
                shopService.add(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    @PutMapping("/update")
    public AjaxResult update(@RequestBody Shop shop){
        /*通过id判断是新增还是删除*/
        try {
                shopService.update(shop);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*删除*/
    @DeleteMapping("/{id}")
    public AjaxResult deleteById(@PathVariable("id")Long id){
        try {
            shopService.deleteById(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*批量删除*/
    @PatchMapping
    public AjaxResult deleteByIds(@RequestBody Long[] ids){
        try {
            shopService.deleteByIds(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            /*反馈操作失败，及具体错误*/
            AjaxResult ajaxResult = AjaxResult.me().setMessage("操作失败" + e.getMessage());
            return ajaxResult;
        }
    }
    /*查询所有*/
    @GetMapping
    public List<Shop> loadAll(){
        return shopService.loadAll();
    }
    /*分页*/
    @PostMapping
    public PageList<Shop> queryPage(@RequestBody ShopQuery query){
        return shopService.queryPage(query);
    }
}
